﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Edm;
using Obsidian.Action;
using Oak.Base;
using Oak.Model;
using Oak.Core.Logic;

namespace Oak.Core.Serv
{
    public class AppKeyServ : OakServ<OakSession>
    {

        private const string APP_KEY_FIELDS = "appKeyId,appKey,desc,modified,created";
        private const string LIST_ATTR_FIELDS = "itemsCount,pageNo,pageSize,pagesCount";

        public AppKeyServ(OakSession session) : base(session) { }

        public void List(ActionRequest req, ActionResponse res)
        {
            CoActAppKeyQry qry = req.GetModelByNameOrFirst<CoActAppKeyQry>("qry");

            AppKeyLG appKeyLG = new AppKeyLG(Session);
            ListAttrInfo listAttr;
            string msg;
            List<CoActAppKeyInfo> list = appKeyLG.GetAppKeyList(qry, out listAttr, out msg);
            if (list == null)
            {
                res.Error(msg);
                return;
            }

            ActionResult rsAppKeys = res.AddResult("appKeys", APP_KEY_FIELDS);
            rsAppKeys.AddModels(list);
            ActionResult rsListAttr = res.AddResult("listAttr", LIST_ATTR_FIELDS);
            rsListAttr.AddModel(listAttr);

        }

        public void Get(ActionRequest req, ActionResponse res)
        {

            ActReqParam param = req.GetParamByNameOrFirst("appKey");
            if (param == null)
            {
                res.Error("参数appKey错误");
                return;
            }
            long appKeyId;
            if (!param.TryGetFirstLong("appKeyId", out appKeyId))
            {
                res.Error("参数appKey属性appKeyId错误");
                return;
            }
            AppKeyLG appKeyLG = new AppKeyLG(Session);
            string msg;
            CoActAppKeyInfo appKey = appKeyLG.GetAppKeyById(appKeyId, out msg);
            if (appKey == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult rsAppKeys = res.AddResult("appKeys", APP_KEY_FIELDS);
            rsAppKeys.AddModel(appKey);
        }


        public void Add(ActionRequest req, ActionResponse res)
        {
            CoActAppKeyInfo appKey = req.GetModelByNameOrFirst<CoActAppKeyInfo>("appKey");
            AppKeyLG appKeyLG = new AppKeyLG(Session);
            string msg;
            appKey = appKeyLG.AddAppKey(appKey, out msg);
            if (appKey == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult rsAppKeys = res.AddResult("appKeys", APP_KEY_FIELDS);
            rsAppKeys.AddModel(appKey);
        }


        public void Update(ActionRequest req, ActionResponse res)
        {
            CoActAppKeyInfo appKey = req.GetModelByNameOrFirst<CoActAppKeyInfo>("appKey");
            AppKeyLG appKeyLG = new AppKeyLG(Session);
            string msg;
            appKey = appKeyLG.UpdateAppKey(appKey, out msg);
            if (appKey == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult rsAppKeys = res.AddResult("appKeys", APP_KEY_FIELDS);
            rsAppKeys.AddModel(appKey);
        }

        public void Delete(ActionRequest req, ActionResponse res)
        {
            ActReqParam param = req.GetParamByNameOrFirst("appKey");
            if (param == null)
            {
                res.Error("参数appKey错误");
                return;
            }
            long appKeyId;
            if (!param.TryGetFirstLong("appKeyId", out appKeyId))
            {
                res.Error("参数appKey属性appKeyId错误");
                return;
            }
            AppKeyLG appKeyLG = new AppKeyLG(Session);
            string msg;
            bool succ = appKeyLG.DeleteAppKey(appKeyId, out msg);
            if (!succ)
            {
                res.Error(msg);
                return;
            }
        }

    }
}
